package com.young.tool;

import com.young.exception.NoDataException;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.prefs.NodeChangeEvent;

/**
 * 描述:全局异常处理（处理所有控制器抛出的异常）
 *
 * @author BluceYoung
 * @create 2020-12-16 11:27
 * B站：TeacherYoung凌风
 * www.teacheryoung.com
 */
@Component
public class GlobalExceptionResolver implements HandlerExceptionResolver {

    @Override
    public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) {
        ModelAndView modelAndView=new ModelAndView();
        //如果异常是NoDataException
        if(ex instanceof NoDataException){
            modelAndView.setViewName("error/404");
            modelAndView.addObject("errMsg",ex.getMessage());
        }
        return modelAndView;
    }
}
